package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.DTO.tencent.ImCallbackRes;
import com.dd.cloud.user.DTO.tencent.ImCallbackUrlParamReq;
import com.dd.cloud.user.DTO.tencent.ImSendmsgSinParamReq;
import com.dd.cloud.user.req.consult.*;
import com.dd.cloud.user.res.chat.ConsultDoctorPageRes;
import com.dd.cloud.user.res.chat.InquiryDateQueueRes;
import com.dd.cloud.user.res.consult.*;
import com.dd.cloud.user.res.doctor.DoctorConsultDetailRes;
import com.dd.cloud.user.res.order.PayOrderResponse;
import com.dd.cloud.user.service.consule.IConsultMedicalRecordService;
import com.dd.cloud.user.service.consule.IConsultSubscribeService;
import com.dd.cloud.user.service.consule.ITencentImMsgService;
import com.dd.cloud.user.service.doctor.IDoctorDetailService;
import com.dd.cloud.user.service.order.IOrderPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 预约问诊 前端控制器
 * </p>
 *
 * @author czg
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/consule")
@Slf4j
@Validated
public class ConsultController {
    @Autowired
    private ITencentImMsgService tencentImMsgService;
    @Autowired
    private IConsultSubscribeService consultSubscribeService;
    @Autowired
    private IDoctorDetailService doctorDetailService;
    @Autowired
    private IConsultMedicalRecordService consultMedicalRecordService;
    @Autowired
    private IOrderPayService orderPayService;

    /**
     * im消息回调
     *
     * @param urlReq url参数
     * @param map    body参数
     * @return
     */
    @PostMapping("/im/callback/msg")
    public ResponseEntity<ImCallbackRes> imSinmsgCallback(HttpServletRequest request, ImCallbackUrlParamReq urlReq, @RequestBody Map map) {
        log.info("微信消息回调:{}", map);
        ImCallbackRes imCallbackRes = tencentImMsgService.imMsgCallback(request, urlReq, map);
        return ResponseEntity.ok(imCallbackRes);
    }

    /**
     * 后台发送消息 测试用
     *
     * @return
     */
    @GetMapping("/tencent/sendTextMsg")
    public ResponseEntity<Void> tencentSendTextMsg(ImSendmsgSinParamReq req) {
        tencentImMsgService.imSendmsgTest(req);
        return ResponseEntity.ok().build();
    }


    /**
     * 后台发送消息 测试用
     *
     * @return
     */
    @GetMapping("/tencent/sendMsg")
    public ResponseEntity<Void> tencentSendMsg(ImSendmsgSinParamReq req) {
        tencentImMsgService.imSendmsgAsymptomSymtem(req);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询问诊排班
     *
     * @return
     */
    @GetMapping("/inquiry/scheduling")
    public ResponseEntity<List<InquiryDateQueueRes>> inquiryScheduling() {
        List<InquiryDateQueueRes> res = consultSubscribeService.inquiryScheduling();
        return ResponseEntity.ok(res);
    }

    /**
     * 查询预约咨询排班
     *
     * @return
     */
    @GetMapping("/consult/scheduling")
    public ResponseEntity<List<InquiryDateQueueRes>> consultScheduling() {
        List<InquiryDateQueueRes> res = consultSubscribeService.consultScheduling();
        return ResponseEntity.ok(res);
    }

    /**
     * 查询能问诊预约的医生列表
     *
     * @return
     */
    @GetMapping("/consult/doctor/page")
    public ResponseEntity<Page<ConsultDoctorPageRes>> inquiryDoctorList(@Valid ConsultDoctorPageReq req) {
        Page<ConsultDoctorPageRes> res = consultSubscribeService.consultDoctorList(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询咨询问诊预约的医生详情
     *
     * @return
     */
    @GetMapping("/consult/doctor/detail")
    public ResponseEntity<DoctorConsultDetailRes> consultDoctorDetail(@NotNull Integer doctorId) {
        DoctorConsultDetailRes res = doctorDetailService.consultDoctorDetail(doctorId);
        return ResponseEntity.ok(res);
    }

    /**
     * 患者预约咨询问诊医生
     *
     * @return
     */
    @PostMapping("/consult/submit")
    public ResponseEntity<PayOrderResponse<?>> consultSubmit(HttpServletRequest request, @Valid @RequestBody ConsultSubscribeCreateReq req) {
        PayOrderResponse<?> res = consultSubscribeService.consultSubmit(request, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询聊天记录
     */
    @GetMapping("/im/record")
    public ResponseEntity<IPage<UserInfoRes>> queryImTencent(@Valid ImTencentRecordReq req) {
        IPage<UserInfoRes> res = consultSubscribeService.queryImTencent(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询历史问诊记录
     *
     * @return
     */
    @GetMapping("/consult/page")
    public ResponseEntity<Page<ConsultSubscribePageRes>> consultPage(@Valid ConsultSubscribePageReq req) {
        Page<ConsultSubscribePageRes> res = consultSubscribeService.consultPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询问诊详情
     *
     * @return
     */
    @GetMapping("/consult/detail")
    public ResponseEntity<ConsultSubscribeDetailRes> consultDetail(@NotNull(message = "问诊id不能为空") Integer id) {
        ConsultSubscribeDetailRes res = consultSubscribeService.consultDetail(id);
        return ResponseEntity.ok(res);
    }

    /**
     * 医生查询患者预约记录
     *
     * @param req
     * @return
     */
    @GetMapping("/consult/page/doctor")
    public ResponseEntity<IPage<ConsultSubscribeDoctorPageRes>> queryConsultSubscribeDoctorPage(@Valid ConsultSubscribeDoctorPageReq req) {
        IPage<ConsultSubscribeDoctorPageRes> res = consultSubscribeService.queryConsultSubscribeDoctorPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 医生接诊
     *
     * @param id
     */
    @PostMapping("/consult/subscribe/receive")
    public ResponseEntity<Void> receivePatientSubscribe(@NotNull(message = "问诊id不能为空") Integer id) {
        consultSubscribeService.receivePatientSubscribe(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 医生结束问诊
     *
     * @param id
     */
    @PostMapping("/consult/subscribe/complete")
    public ResponseEntity<Void> completePatientSubscribe(@NotNull(message = "问诊id不能为空") Integer id) {
        consultSubscribeService.completePatientSubscribe(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 延迟问诊时长 参数分钟
     *
     * @param id
     * @param min
     */
    @PostMapping("/consult/subscribe/delayed")
    public ResponseEntity<LocalDateTime> patientSubscribeDelayed(@NotNull(message = "问诊id不能为空") Integer id, @NotNull(message = "延长时长不能为空") Integer min) {
        LocalDateTime res = consultSubscribeService.patientSubscribeDelayed(id, min);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询医生待办消息
     *
     * @param doctorId
     * @return
     */
    @GetMapping("/consult/msg/unread/count")
    public ResponseEntity<ConsultMsgUnReadCountRes> queryConsultMsgUnReadCountDoctor(@RequestParam(required = false) Integer doctorId) {
        ConsultMsgUnReadCountRes res = consultSubscribeService.queryConsultMsgUnReadCountDoctor(doctorId);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询医生在线问诊在线咨询待接诊数量
     *
     * @param doctorId
     * @return
     */
    @GetMapping("/consult/msg/un/receive/count")
    public ResponseEntity<ConsultUnReceiveCountRes> queryConsultUnReceiveCountDoctor(@RequestParam(required = false) Integer doctorId) {
        ConsultUnReceiveCountRes res = consultSubscribeService.queryConsultUnReceiveCountDoctor(doctorId);
        return ResponseEntity.ok(res);
    }

    /**
     * 创建问诊电子病历
     *
     * @param req
     */
    @PostMapping("/consult/medical/record/create")
    public ResponseEntity<Void> createConsultMedicalRecord(@Valid @RequestBody ConsultMedicalRecordCreateReq req) {
        consultMedicalRecordService.createConsultMedicalRecord(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询问诊电子病历列表
     *
     * @param req
     * @return
     */
    @GetMapping("/consult/medical/record/page")
    public ResponseEntity<IPage<ConsultMedicalRecordPageRes>> queryConsultMedicalRecordPage(@Valid ConsultMedicalRecordPageReq req) {
        IPage<ConsultMedicalRecordPageRes> res = consultMedicalRecordService.queryConsultMedicalRecordPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 订单退款
     *
     * @param orderNo
     */
    @PostMapping(value = "/refund/order")
    public ResponseEntity<Void> refundOrder(@NotBlank(message = "订单编号不能为空") String orderNo) {
        orderPayService.refundOrder(orderNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除订单
     *
     * @param orderNo
     */
    @PostMapping(value = "/update/status")
    public ResponseEntity<Void> updateOrderStatus(@NotBlank(message = "订单编号不能为空") String orderNo) {
        orderPayService.updateOrderStatus(orderNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 未支付方订单重新支付
     *
     * @param req 订单编号
     */
    @PostMapping(value = "/order/payagain")
    public ResponseEntity<PayOrderResponse<?>> orderPayagain(HttpServletRequest request, @Valid @RequestBody ConsultNotPayReq req) {
        PayOrderResponse<?> res = orderPayService.orderPayagain(request, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 测试退款使用
     *
     * @param orderNo
     */
    @PostMapping(value = "/test/refund/order")
    public ResponseEntity<Void> testRefundOrder(@NotBlank(message = "订单编号不能为空") String orderNo) {
        orderPayService.testRefundOrder(orderNo);
        return ResponseEntity.ok().build();
    }

    /**
     * 保存患者所需症状
     */
    @PostMapping("/add/symptom")
    public ResponseEntity<Void> saveSymptom(@Valid @RequestBody ConsultSysmptomReq consultSysmptomReq) {
        consultSubscribeService.saveSymptom(consultSysmptomReq);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取症状
     */
    @GetMapping("/query/symptom")
    public ResponseEntity<String> querySymptom(@RequestParam("consultId") Integer consultId) {
        String res = consultSubscribeService.querySymptom(consultId);
        return ResponseEntity.ok(res);
    }

    /**
     * 查询接诊数量
     *
     * @param docId
     * @return
     */
    @GetMapping("/query/receive/count")
    public ResponseEntity<Integer> queryReceiveConsultCount(@RequestParam(required = false) Integer docId) {
        Integer res = consultSubscribeService.queryReceiveConsultCount(docId);
        return ResponseEntity.ok(res);
    }
    /**
     * 查询患者待办消息
     *
     * @return
     */
    @GetMapping("/consult/user/msg/unread/count")
    public ResponseEntity<ConsultMsgUnReadCountRes> queryConsultMsgUnReadCountUser() {
        ConsultMsgUnReadCountRes res = consultSubscribeService.queryConsultMsgUnReadCountUser();
        return ResponseEntity.ok(res);
    }

}

